﻿<Window x:Class="ULSDeobfuscator.LogFileChooser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:ULSDeobfuscator="clr-namespace:ULSDeobfuscator"
        xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:MvvmHelpers="clr-namespace:Elca.MvvmHelpers" 
        xmlns:Behaviours="clr-namespace:FunctionalFun.UI.Behaviours"
        mc:Ignorable="d" 
        d:DataContext="{d:DesignInstance Type=ULSDeobfuscator:LogFileChooserViewModel}"
        d:DesignHeight="800"
        d:DesignWidth="620"
        Left="{Binding WindowLeft, Mode=TwoWay}"
        Top="{Binding WindowTop, Mode=TwoWay}"

        Title="Choose one or more SharePoint 2007/2010/2013 Log File(s)"
      
        MvvmHelpers:LifetimeEvent.Loaded ="{Binding LoadCommand}" 
        MvvmHelpers:DropHelper.DropHandler="{Binding}" 
        
        ShowInTaskbar="{Binding IsWindowVisibleInTaskbar, Mode=OneTime}">

    <Window.Resources>

        <CollectionViewSource x:Key="LogFilesSource"
                              Source="{Binding LogFiles}">
            <CollectionViewSource.SortDescriptions>
                <ComponentModel:SortDescription PropertyName="HostName" />
                <ComponentModel:SortDescription PropertyName="DateTime" Direction="Descending"  />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <CollectionViewSource x:Key="RecentLogFilesSource"
                              Source="{Binding RecentLogFiles}">
            <CollectionViewSource.SortDescriptions>
                <ComponentModel:SortDescription PropertyName="HostName" />
                <ComponentModel:SortDescription PropertyName="DateTime" Direction="Descending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <CollectionViewSource x:Key="RecentDirectoriesSource"
                              Source="{Binding RecentDirectories}">
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <!-- 1: explanation -->
            <RowDefinition Height="Auto" />
            <!-- 2: SharePoint logs directory-->
            <RowDefinition Height="Auto" />
            <!-- 3: chosen logs directory -->
            <RowDefinition Height="Auto" />
            <!-- 4: available logs file-->
            <RowDefinition Height="*" />
            <!-- 5: date and time -->
            <RowDefinition Height="Auto" />
            <!-- 6: explicit file -->
            <RowDefinition Height="Auto" />
            <!-- 7: old files -->
            <RowDefinition Height="Auto" />
            <!-- 8: add/replace -->
            <RowDefinition Height="Auto" />
            <!-- 9: OK/Cancel-->
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0"
                   Margin="5" Text="{Binding Version}"/>
            
        <TextBlock Grid.Row="1"
                   Margin="5"
                   TextWrapping="Wrap"
                   Text="Choose a directory to scan for log files, then the log file corresponding to the time period to analyze.&#x0a;Or enter a date and time; this chooses the file in the logs directory most likely to contain this moment.&#x0a;Or explicitly browse for a file.&#x0a;Or choose a recently-opened file." />

        <!-- 2: SharePoint logs directory-->
        <Grid Grid.Row="2"
              Margin="5">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Width="483">
                <Label Content="_SharePoint logs directory: "
                       Target="{Binding ElementName=sharePointLogsDirectory}"
                       VerticalAlignment="Center" />
                <TextBox Text="{Binding SharePointLogsDirectory, Mode=OneWay}"
                         x:Name="sharePointLogsDirectory"
                         IsReadOnly="True"
                         />
            </StackPanel>
            <Button Grid.Column="1"
                    Margin="0,0,5,0"
                    Command="{Binding UseSharePointLogsDirectoryCommand}"
                    Style="{StaticResource LargeButtonStyle}"
                    HorizontalAlignment="Right">
                Use it
            </Button>
        </Grid>

        <!-- 3: chosen logs directory -->
        <Grid Grid.Row="3"
              Margin="5">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="402*" />
                <ColumnDefinition Width="85*" />
            </Grid.ColumnDefinitions>

            <Label Content="_Logs directory: "
                   Target="{Binding ElementName=dirNames}"
                   VerticalAlignment="Center"/>
            <ComboBox Grid.Column="1"
                      x:Name="dirNames"
                      Margin="5,0,5,0"
                      IsEditable="True"
                      Text="{Binding CurrentLogsPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      ItemsSource="{Binding Source={StaticResource RecentDirectoriesSource}}" />
            <Button Grid.Column="2"
                    Margin="0,0,5,0"
                    Style="{StaticResource LargeButtonStyle}"
                    Command="{Binding BrowseForLogsDirectory}" >
                Browse...
            </Button>
        </Grid>

        <!-- 4: available logs file-->
        <RadioButton Grid.Row="4"
                     Margin="5"
                     MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsFileFromDirectorySelected, Mode=TwoWay}"
                     >
            <RadioButton.Content>
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Label Content="_Directory contents:" 
                           Target="{Binding ElementName=dirNames}"
                           Margin="0,0,0,4"/>
                    <ListView ItemsSource="{Binding Source={StaticResource LogFilesSource}}"
                              Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedLogFilesFromDirectory}"
                              MvvmHelpers:DoubleClickHelper.Command="{Binding OpenCommand}"
                              MvvmHelpers:GridViewSort.AutoSort="True"
                              Margin="5,0,0,0"
                              Grid.Row="1">
                        <ListView.View>
                            <GridView AllowsColumnReorder="False" >
                                <GridView.Columns>
                                    <GridViewColumn Header="Host" Width="{Binding HostNameWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding HostName, Mode=OneTime}"
                                                    MvvmHelpers:GridViewSort.PropertyName="HostName"
                                                    />
                                    <GridViewColumn Header="Date" Width="{Binding DayWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding DisplayedDay, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="DateTime"
                                                    />
                                    <GridViewColumn Header="Start Time" Width="{Binding TimeWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding DisplayedTime, Mode=OneTime}" 
                                                    />
                                    <GridViewColumn Header="File Size" Width="{Binding SizeWidth, Mode=TwoWay}" 
                                                    DisplayMemberBinding="{Binding FileSize, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="OriginalFile.Length"
                                                    />
                                    <GridViewColumn Header="Original Filename" Width="{Binding OriginalFileWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding OriginalFile.Name, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="OriginalFile.Name"
                                                    />
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>
                    <TextBlock Grid.Row="2"
                               Margin="0,5,0,0"
                               Text="When no content is shown here, it may mean that the viewer should be run with administrative rights"
                               Visibility="{Binding EmptyExplanationTextVisibility}"/>
                </Grid>
            </RadioButton.Content>
        </RadioButton>

        <!-- 5: date and time -->
        <RadioButton Grid.Row="5"
                     Margin="5"
                     MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsDateTimeSelected, Mode=TwoWay}"
                     >

            <RadioButton.Content>
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="0"
                           Content="_Enter a date and time to analyze"
                           Target="{Binding ElementName=logTime}"
                           Margin="0,0,0,4"/>

                    <TextBox Grid.Row="1" 
                             x:Name="logTime"
                             Margin="5,0,0,0"
                             Text="{Binding SelectedLogDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
                             />
                    <Button Grid.Row="0" Grid.Column="1"
                            Width="50"
                            VerticalAlignment="Center"
                            Padding="4"
                            HorizontalAlignment="Left"
                            Margin="25,0,0,0"
                            Command="{Binding RefreshAnalysisTimeCommand}"
                    >
                        Now
                    </Button>
                    <TextBlock Grid.Row="1" Grid.Column="1"
                               Margin="5,0,0,0"
                               VerticalAlignment="Center"
                               TextWrapping="Wrap"
                               Text="{Binding ClosestLogFileExplanation, Mode=OneWay}" />
                </Grid>
            </RadioButton.Content>
        </RadioButton>

        <!-- 6: explicit file -->
        <RadioButton Grid.Row="6"
                     Margin="5" 
                     HorizontalContentAlignment="Stretch"
                     MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsExplicitFileSelected, Mode=TwoWay}"
                     >

            <RadioButton.Content>
                <Grid >

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="Choose the right file to display yourself:"
                               Grid.ColumnSpan="3"
                               Margin="5,0,0,4"/>

                    <Label Content="Log _file: "
                           Grid.Row="1" Grid.Column="0"
                           Target="{Binding ElementName=explicitLogFile}"
                           VerticalAlignment="Center"/>
                    <TextBox Grid.Row="1" Grid.Column="1"
                             x:Name="explicitLogFile"
                             Margin="5,0,5,0"
                             Text="{Binding ExplicitLogFilePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Button Grid.Row="1" Grid.Column="2"
                            Margin="0,0,5,0"
                            Style="{StaticResource LargeButtonStyle}"
                            Command="{Binding BrowseForExplicitLogFileCommand}"
                            HorizontalAlignment="Right">
                        Browse...
                    </Button>
                </Grid>
            </RadioButton.Content>
        </RadioButton>

        <!-- 7: old files -->
        <RadioButton Grid.Row="7"
                     Margin="5"
                     IsEnabled="{Binding IsRecentLogFilesSelectable}"
                     MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsRecentLogFileSelected, Mode=TwoWay}"
                     >

            <RadioButton.Content>
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <Label Content="Recently _opened files:" 
                           Target="{Binding ElementName=recentLogFiles}"
                           Margin="0,0,0,4"/>

                    <ListView ItemsSource="{Binding Source={StaticResource RecentLogFilesSource}}"
                              Behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding RecentSelectedLogFiles}"
                              MvvmHelpers:DoubleClickHelper.Command="{Binding OpenCommand}"
                              MvvmHelpers:GridViewSort.AutoSort="True"
                              x:Name="recentLogFiles"
                              Margin="5,0,0,0"
                              Grid.Row="1">
                        <ListView.View>
                            <GridView AllowsColumnReorder="False">
                                <GridView.Columns>
                                    <GridViewColumn Header="Host" Width="{Binding HostNameWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding HostName, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="HostName"
                                                    />
                                    <GridViewColumn Header="Date" Width="{Binding DayWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding DisplayedDay, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="DateTime"
                                                    />
                                    <GridViewColumn Header="Start Time" Width="{Binding TimeWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding DisplayedTime, Mode=OneTime}" 
                                                    />
                                    <GridViewColumn Header="File Size" Width="{Binding SizeWidth, Mode=TwoWay}" 
                                                    DisplayMemberBinding="{Binding FileSize, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="OriginalFile.Length"
                                                    />
                                    <GridViewColumn Header="Original Filename" Width="{Binding OriginalFileWidth, Mode=TwoWay}"
                                                    DisplayMemberBinding="{Binding OriginalFile.Name, Mode=OneTime}" 
                                                    MvvmHelpers:GridViewSort.PropertyName="OriginalFile.Name"
                                                    />
                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </RadioButton.Content>
        </RadioButton>

        <StackPanel Grid.Row="8"
                    Margin="20,10,0,0"
                    Visibility="{Binding ChooseAdditionOrReplaceVisibility}"
                    Orientation="Horizontal">
            <RadioButton Content="_Add to existing log display"
                         GroupName="AddReplace"
                         MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsLogAddition, Mode=TwoWay}"
                         />
            <RadioButton Content="_Replace existing log display" 
                         GroupName="AddReplace"
                         MvvmHelpers:RadioButtonHelper.IsChecked="{Binding IsLogReplacement, Mode=TwoWay}"
                         Margin="20,0,0,0" />
        </StackPanel>
        
        <!-- 9: OK/Cancel-->
        <Grid Grid.Row="9"
              Margin="5,25,5,5">
            <CheckBox Content="Remo_ve duplicate lines"
                      Margin="5"
                      HorizontalAlignment="Left"
                      IsChecked="{Binding IsDuplicateRemovalEnabled, Mode=TwoWay}" />

            <StackPanel HorizontalAlignment="Right"
                        Orientation="Horizontal"
                        Margin="0, 0, 10, 0">
                <Button Command="{Binding OpenCommand}"
                        Style="{StaticResource LargeButtonStyle}"
                        IsDefault="True"
                        Content="Open" />
                <Button Command="{Binding CancelCommand}"
                        Style="{StaticResource LargeButtonStyle}"
                        IsCancel="True"
                        Margin="20, 0, 0, 0"
                        Content="Cancel" />
            </StackPanel>
        </Grid>
    </Grid>
</Window>
